In [ ]:
import altair as alt
import pandas as pd
import geopandas as gpd
In [ ]:
df = pd.read_csv('./population_trends.csv')
df = df[df.region != 'Ukraine']
df_last_year = df[df['year'] == 2019]
df_last_year = df_last_year.fillna(0)

ukraine = gpd.read_file('./ukraine.json')
ukraine.head()
Out[ ]:
GID_0 NAME_0 GID_1 NAME_1 VARNAME_1 NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1 HASC_1 geometry
0 UKR Ukraine UKR.1_1 Cherkasy Cherkas'ka Oblast'|Cherkasskaya Oblast'|Cherkassy None Oblast' Region None UA.CK MULTIPOLYGON (((31.32614 48.74507, 31.31716 48...
1 UKR Ukraine UKR.2_1 Chernihiv Chernigov|Tschernigow None Oblast' Region None UA.CH MULTIPOLYGON (((33.09283 50.50966, 33.09261 50...
2 UKR Ukraine UKR.3_1 Chernivtsi Chernivets'ka Oblast'|Chernovitskaya Oblast'|C... None Oblast' Region None UA.CV MULTIPOLYGON (((24.93280 47.72794, 24.93301 47...
3 UKR Ukraine UKR.4_1 Crimea Crimée|Criméia|Krim|Krymskaya Respublika|Respu... None Autonomous Republic Autonomous Republic None UA.KR MULTIPOLYGON (((33.79291 44.39153, 33.79465 44...
4 UKR Ukraine UKR.5_1 Dnipropetrovs'k Dnipropetrovsk|Dniepropietrovsk|Dnjepropetrowsk None Oblast' Region None UA.DP MULTIPOLYGON (((33.93176 47.48407, 33.92332 47...
In [ ]:
select_region = alt.selection_single(on = 'mouseover', empty = 'all',  fields=['region'])

base = alt.Chart(ukraine).transform_lookup(
    lookup = 'NAME_1',
    from_ = alt.LookupData(data = df_last_year,
                           key = 'region',
                           fields=['year', 'rate', 'region']
                           )
).mark_geoshape(stroke = 'white', strokeWidth = 1
).encode(
  color = alt.Color('rate:Q', scale = alt.Scale(scheme = 'bluepurple', reverse=True)),
  opacity = alt.condition(
    select_region,
    alt.value(1),
    alt.value(0.3)
),
).add_selection(
    select_region
).properties(width = 800, height = 600).encode(tooltip = [
        alt.Tooltip('region:N'),
        alt.Tooltip('rate:N')
    ])
In [ ]:
linechart = alt.Chart(df).mark_line().encode(
    x = alt.X('year:Q', 
              scale = alt.Scale(
                                domain = [df.year.min(), 
                                          df.year.max()]),
            axis = alt.Axis(
             format = 'k',
             )),
    y = alt.Y('rate:Q', 
              scale = alt.Scale(zero = False,
                                domain = [df.rate.min(), 
                                          df.rate.max()])),
color = alt.condition(
    select_region,
    alt.ColorValue('#A600BD'), alt.ColorValue('#A9A9A9')
),
strokeWidth=alt.condition(
        select_region,
        alt.value(2),
        alt.value(0.5))
,
    detail = alt.Detail('region:N'),
).properties(width = 900, height = 600)
In [ ]:
alt.hconcat(base.properties(width = 750, height = 500),
            linechart.properties(width = 450, height = 500)
           ).add_selection(select_region).properties(background = '#F9F9F9', padding = 25, 
           title = alt.TitleParams(
        text = 'Рівень приросту / скорочення населення України за регіонами', dx=350, dy=-30
    )).configure_title(
    anchor = 'start',
    frame = 'group',
    fontSize = 18,
    subtitleFontSize = 16
)
Out[ ]:
In [ ]: